package com.example.cyclops;

import java.util.ArrayList;
import java.util.List;

import org.libsdl.app.MythHardwareDecoder;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class MythListView extends ListActivity {

	private ArrayList<PDFOutlineElement> mPdfOutlinesCount = new ArrayList<PDFOutlineElement>();
	private ArrayList<PDFOutlineElement> mPdfOutlines = new ArrayList<PDFOutlineElement>();
	private TreeViewAdapter treeViewAdapter = null;
	private String UserID;
	public static MythListView instance = null;
	boolean isExit = false;
	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			isExit = false;
		}

	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		/*
		 * 
		 * if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
		 * { //获取 back键
		 * 
		 * if(menu_display){ //如果 Menu已经打开 ，先关闭Menu menuWindow.dismiss();
		 * menu_display = false; } else { Intent intent = new Intent();
		 * intent.setClass(MainWeixin.this,Exit.class); startActivity(intent); }
		 * }
		 */
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

			Intent intent = new Intent();
			intent.setClass(MythListView.this, Exit.class);
			startActivity(intent);

			/*
			 * if (!isExit) { isExit = true;
			 * Toast.makeText(getApplicationContext(), "再按一次退出程序",
			 * Toast.LENGTH_SHORT).show(); // 利用handler延迟发送更改状态信息
			 * mHandler.sendEmptyMessageDelayed(0, 2000); } else { finish();
			 * System.exit(0); }
			 */
		}
		return false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		instance = this;
		mPdfOutlines = LoadingActivity.mPdfOutlines;
		mPdfOutlinesCount = LoadingActivity.mPdfOutlinesCount;
		initialData();
		treeViewAdapter = new TreeViewAdapter(this, R.layout.outline,
				mPdfOutlinesCount);
		setListAdapter(treeViewAdapter);
		registerForContextMenu(getListView());
	}

	private void initialData() {
		// this is only for test
	}

	private class TreeViewAdapter extends ArrayAdapter {

		public TreeViewAdapter(Context context, int textViewResourceId,
				List objects) {
			super(context, textViewResourceId, objects);
			mInflater = LayoutInflater.from(context);
			mfilelist = objects;
			mIconCollapse = BitmapFactory.decodeResource(
					context.getResources(), R.drawable.outline_list_collapse);
			mIconExpand = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.outline_list_expand);
			mIconThird = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.first);
			mIconSecond = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.second);
			mIconPtz = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.ptz);
			mIconNoPtz = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.noptz);
		}

		private LayoutInflater mInflater;
		private List<PDFOutlineElement> mfilelist;
		private Bitmap mIconCollapse;
		private Bitmap mIconExpand;
		private Bitmap mIconThird;
		private Bitmap mIconSecond;
		private Bitmap mIconPtz;
		private Bitmap mIconNoPtz;

		public int getCount() {
			return mfilelist.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		// 图片放大的方法
		private Bitmap big(Bitmap bmp, float scale) {
			int bmpWidth = bmp.getWidth();
			int bmpHeight = bmp.getHeight();
			// 计算出这次要缩小的比例
			// float scaleWidth = (float) (bmpWidth * scale);
			// float scaleHeight = (float) (bmpHeight * scale);
			// 产生ReSize之后的bmp对象
			Matrix matrix = new Matrix();
			matrix.postScale(scale, scale);
			Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth,
					bmpHeight, matrix, true);
			return resizeBmp;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;
			/* if (convertView == null) { */
			convertView = mInflater.inflate(R.layout.outline, null);
			holder = new ViewHolder();
			holder.text = (TextView) convertView.findViewById(R.id.text);
			holder.text.setTextColor(0xFF000000);
			// holder.text.setTextSize(22);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			convertView.setTag(holder);
			/*
			 * } else { holder = (ViewHolder) convertView.getTag(); }
			 */

			int level = mfilelist.get(position).getLevel();
			holder.icon.setPadding(25 * (level + 1),
					holder.icon.getPaddingTop(), 0,
					holder.icon.getPaddingBottom());
			holder.text.setText(mfilelist.get(position).getOutlineTitle());
			if (mfilelist.get(position).isMhasChild()) {
				if (mfilelist.get(position).isTop())
					holder.icon.setImageBitmap(big(this.mIconThird, 1.5f));
				else
					holder.icon.setImageBitmap(big(this.mIconSecond, 1.5f));

			} else if (!mfilelist.get(position).isMhasChild()) {
				if (mfilelist.get(position).isPTZ())
					holder.icon.setImageBitmap(mIconPtz);
				else
					holder.icon.setImageBitmap(big(this.mIconNoPtz, 1.5f));
				// holder.icon.setVisibility(View.INVISIBLE);
			}
			return convertView;
		}

		class ViewHolder {
			TextView text;
			ImageView icon;

		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		if (!mPdfOutlinesCount.get(position).isMhasChild()) {
			// Toast.makeText(this,
			// mPdfOutlinesCount.get(position).getOutlineTitle(), 2000);
			// i have found it!

			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			PDFOutlineElement element = mPdfOutlinesCount.get(position);
			bundle.putString("ip", element.getIP());
			bundle.putString("cameraid", element.getCameraID());

			//bundle.putString("ip", "192.168.1.104");
			//bundle.putString("cameraid", "10023");
			bundle.putString("cameratext", element.getOutlineTitle());
			intent.putExtras(bundle);
			intent.setClass(MythListView.this, MythHardwareDecoder.class);
			/*
			switch (Global.displaymode) {
			case 0:
				intent.setClass(MythListView.this, MythHardwareDecoder.class);
				break;
			case 1:
				intent.setClass(MythListView.this, SDLActivity.class);
				break;
			case 2:
				intent.setClass(MythListView.this, JNITest.class);
				break;
			}
			*/
			startActivity(intent);
			return;
		}

		if (mPdfOutlinesCount.get(position).isExpanded()) {
			mPdfOutlinesCount.get(position).setExpanded(false);
			PDFOutlineElement pdfOutlineElement = mPdfOutlinesCount
					.get(position);
			ArrayList<PDFOutlineElement> temp = new ArrayList<PDFOutlineElement>();

			for (int i = position + 1; i < mPdfOutlinesCount.size(); i++) {
				if (pdfOutlineElement.getLevel() >= mPdfOutlinesCount.get(i)
						.getLevel()) {
					break;
				}
				temp.add(mPdfOutlinesCount.get(i));
			}

			mPdfOutlinesCount.removeAll(temp);

			treeViewAdapter.notifyDataSetChanged();
			/*
			 * fileExploreAdapter = new TreeViewAdapter(this, R.layout.outline,
			 * mPdfOutlinesCount);
			 */

			// setListAdapter(fileExploreAdapter);

		} else {
			mPdfOutlinesCount.get(position).setExpanded(true);
			int level = mPdfOutlinesCount.get(position).getLevel();
			int nextLevel = level + 1;

			for (PDFOutlineElement pdfOutlineElement : mPdfOutlines) {
				int j = 1;
				if (pdfOutlineElement.getParent().equals(
						mPdfOutlinesCount.get(position).getId())) {
					pdfOutlineElement.setLevel(nextLevel);
					pdfOutlineElement.setExpanded(false);
					mPdfOutlinesCount.add(position + j, pdfOutlineElement);
					j++;
				}
			}
			treeViewAdapter.notifyDataSetChanged();
			/*
			 * fileExploreAdapter = new TreeViewAdapter(this, R.layout.outline,
			 * mPdfOutlinesCount);
			 */

			// setListAdapter(fileExploreAdapter);
		}
	}
}
